/*------------------------------------------------------------------------
# Analyst - May 12, 2013
# ------------------------------------------------------------------------
# Designed by BestWebSoft & HTML by MegaDrupal
# Websites:  http://www.megadrupal.com -  Email: info@megadrupal.com
--------------------------------------------------------------------------*/

function scrollBanner() {
    //Get the scoll position of the page
    scrollPos = $(this).scrollTop();
    //Scroll the background of the banner
    $('#header').css({
        'background-position': 'center ' + (-scrollPos / 8) + "px"
    });
  }
function isMobile(){
    return (
        (navigator.userAgent.match(/Android/i)) ||
		(navigator.userAgent.match(/webOS/i)) ||
		(navigator.userAgent.match(/iPhone/i)) ||
		(navigator.userAgent.match(/iPod/i)) ||
		(navigator.userAgent.match(/iPad/i)) ||
		(navigator.userAgent.match(/BlackBerry/))
    );
}

$(function(){
    var sys_about_testimonial = $("#sys_about_testimonial"),
        sys_indicators = $("#sys_indicators"),
        sys_lst_people_name = $("#sys_lst_people_name"),
        sys_present_lst = $("#sys_present_lst");
    if(!isMobile()) {
        $(window).scroll(function() {
           scrollBanner();
        });
    }
		
		/* Sequence js
		---------------------------------------------------------- */	
    var options = {
        pagination: true,
        animateStartingFrameIn: true,
        autoPlay: true,
        autoPlayDelay: 6500,
        preloader: true,
        preloadTheseFrames: [1],
        preloadTheseImages: []
    };
    var mySequence = $("#sequence").sequence(options).data("sequence");
		
		/* get Twitter
		---------------------------------------------------------- */	
    getTwitters('sys_lst_tweets', {
        id: 'megadrupal',
        count: 2,
        template: '<div class="tweet-item"><p class="rs tweet-mind">%text%</p><p class="rs timestamp">posted %time%</p><i class="icon iTwitter"></i></div>'
    });

		/* Testimonial slide
		---------------------------------------------------------- */	
    if(sys_about_testimonial.length>0) {
        sys_indicators.on("click",".dot-gray",function(){
            var getIdx=$(this).index();
            sys_indicators.children().removeClass("active").end().children(":eq(" + getIdx + ")").addClass("active");
            sys_present_lst.children().removeClass("active").hide();
            sys_present_lst.children(":eq(" + getIdx + ")").fadeIn(function() {
                $(this).addClass("active");
            });
            sys_lst_people_name.children().removeClass("active").hide();
            sys_lst_people_name.children(":eq(" + getIdx + ")").fadeIn(function() {
                $(this).addClass("active");
            });
        });
    }
    function autoTestimonial() {
        var getIdx = sys_present_lst.children(".active").index(),
            lengthTestimonial = sys_present_lst.children().length;
        getIdx++;
        if(getIdx==lengthTestimonial)
            getIdx = 0;
        sys_indicators.children().removeClass("active").end().children(":eq(" + getIdx + ")").addClass("active");
        sys_present_lst.children().removeClass("active").hide();
        sys_present_lst.children(":eq(" + getIdx + ")").fadeIn(function() {
            $(this).addClass("active");
        });
        sys_lst_people_name.children().removeClass("active").hide();
        sys_lst_people_name.children(":eq(" + getIdx + ")").fadeIn(function() {
            $(this).addClass("active");
        });
    }
    setInterval(function(){
        autoTestimonial();
    },6000);


		/* Navigation on mobile (Sidr)
		---------------------------------------------------------- */	
    $('#btn-toogle-menu').sidr({
        side:"right",
        name:"alternate-menu",
        source:"#right-menu"
    });

    /* Loadmore button on blog
		---------------------------------------------------------- */	
    $('#showmorepost').bind('click', function (e) {
        _self = $(this);
        _self.text('Loading...')
        $.ajax({
            url: "ajax/blog.php"
        }).done(function(data) {
            $('#main-content').append(data);
            _self.text('Show more')
        });
        return false;
    });
	
	/* Contact form: Ajax & Validate
	---------------------------------------------------------- */	
	if(jQuery("#contact-form").length > 0){
        // Validate the contact form
        jQuery('#contact-form').validate({
	
            // Add requirements to each of the fields
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                email: {
                    required: true,
                    email: true
                },
                message: {
                    required: true,
                    minlength: 10
                }
            },
		
            // Specify what error messages to display
            // when the user does something horrid
            messages: {
                name: {
                    required: "Please enter your name.",
                    minlength: jQuery.format("At least {0} characters required.")
                },
                email: {
                    required: "Please enter your email.",
                    email: "Please enter a valid email."
                },
                message: {
                    required: "Please enter a message.",
                    minlength: jQuery.format("At least {0} characters required.")
                }
            },
		
            // Use Ajax to send everything to processForm.php
            submitHandler: function(form) {
                jQuery("#submit-contact").attr("value", "Sending...");
                jQuery(form).ajaxSubmit({
                    success: function(responseText, statusText, xhr, $form) {
                        jQuery("#response").html(responseText).hide().slideDown("fast");
                        jQuery("#submit-contact").attr("value", "Submit");
                        jQuery(form).find("input[type=text]").val('');
                        jQuery(form).find("input[type=email]").val('');
                        jQuery(form).find("input[type=url]").val('');
                        jQuery(form).find("textarea").val('');
                    }
                });
                return false;
            }
        });
    }

});